package com.hzncc.scaffold.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzncc.scaffold.config.MinioConfig;
import com.hzncc.scaffold.core.ResultT;
import com.hzncc.scaffold.entity.Other.News;
import com.hzncc.scaffold.entity.Vo.AttachFileVo;
import com.hzncc.scaffold.entity.Vo.NewsDetailVo;
import com.hzncc.scaffold.mapper.NewsMapper;
import com.hzncc.scaffold.service.NewsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzncc.scaffold.utils.MinioUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 新闻表 服务实现类
 * </p>
 *
 * @author DrunkBear
 * @since 2022-04-27
 */
@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService {

    @Autowired
    NewsService newsService;



    @Override
    public ResultT saveNews(News news) {
        news.setPubTime("0000-00-00 00:00:00");
        news.setSortTime(DateUtil.now());
        news.setStatus(0);
        boolean save = newsService.save(news);
        if (save) {
            return ResultT.success("新增成功");
        } else {
            return ResultT.fail("新增失败");
        }
    }

    @Override
    public ResultT updateNews(News news) {

        //状态从未发表变为已发布时 记录发布时间与发布人
//        News oldNews = newsService.getById(news.getId());
        LambdaQueryWrapper<News> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(News::getId,news.getId());
        News one = newsService.getOne(queryWrapper);
        if (one.getStatus() == 0 &&  news.getStatus() != null && news.getStatus() == 1) {
            news.setPubTime(DateUtil.now());
            news.setPublisherId(StpUtil.getLoginIdAsInt());
        }

        //当状态从已撤回变为已发布时 更新发布时间与发布人
        if(news.getStatus()!=null&&one.getStatus()!=null){
            if (one.getStatus() == 2 && news.getStatus() == 1) {
                news.setPubTime(DateUtil.now());
            }
        }
        boolean update = newsService.updateById(news);
        if (update) {
            return ResultT.success("更新成功");
        } else {
            return ResultT.fail("更新失败");
        }
    }

    @Override
    public ResultT stayAtTop(Integer newsId, Integer stayAtTop) {
        if (newsService.getById(newsId) != null) {
            News news = newsService.getById(newsId);
            if (stayAtTop == 0 || stayAtTop == 1) {
                news.setStayAtTop(stayAtTop);
                news.setSortTime(DateUtil.now());
            } else {
                return ResultT.fail("数据错误");
            }
            boolean updateById = newsService.updateById(news);
            if (updateById) {
                if (stayAtTop == 1) {
                    return ResultT.success("置顶成功");
                } else {
                    return ResultT.success("取消置顶成功");
                }
            } else {
                return ResultT.fail("置顶失败");
            }
        } else {
            return ResultT.fail("该资讯不存在");
        }
    }

    @Override
    public List<News> queryByEntity(News news) {
        LambdaQueryWrapper<News> lqw = new LambdaQueryWrapper<>();
        lqw.eq(news.getId() != null, News::getId, news.getId());
        lqw.eq(news.getTitle() != null, News::getTitle, news.getTitle());
        lqw.eq(news.getStatus() != null, News::getStatus, news.getStatus());
        lqw.eq(news.getPublisherId() != null, News::getPublisherId, news.getPublisherId());
        lqw.eq(news.getPubTime() != null, News::getPubTime, news.getPubTime());
        lqw.eq(news.getContent() != null, News::getContent, news.getContent());
        lqw.eq(news.getType() != null, News::getType, news.getType());
        lqw.eq(news.getStayAtTop() != null, News::getStayAtTop, news.getStayAtTop());
        lqw.eq(news.getSortTime() != null, News::getSortTime, news.getSortTime());
        lqw.eq(news.getInfo() != null, News::getInfo, news.getInfo());
        lqw.eq(news.getIsTrainingClass() != null, News::getIsTrainingClass, news.getIsTrainingClass());
        lqw.eq(news.getTrainingClassId() != null, News::getTrainingClassId, news.getTrainingClassId());
        lqw.eq(news.getAttachFile() != null, News::getAttachFile, news.getAttachFile());

        List<News> newsList = newsService.list(lqw);
        return newsList;
    }


    @Autowired
    private MinioConfig minioConfig;

    @Value("${project.imgFileSuffix}")
    public String imgFileSuffix;

    @Override
    public ResultT detail(Integer newsId) {
        if (newsService.getById(newsId) != null) {
            NewsDetailVo newsDetailVo = new NewsDetailVo();
            News news = newsService.getById(newsId);
            BeanUtil.copyProperties(news, newsDetailVo, "attachFile");

            if (StrUtil.isNotEmpty(news.getAttachFile())) {
                List<AttachFileVo> attachFileVoList = new ArrayList<>();
                try {
                    JSONArray jsonArray = new JSONArray(news.getAttachFile());
                    attachFileVoList = jsonArray.toList(AttachFileVo.class);
                }catch (Exception e){
                    e.printStackTrace();
                }
                for (AttachFileVo attachFileVo : attachFileVoList) {
                    attachFileVo.setPath(imgFileSuffix+attachFileVo.getPath());
                }
                newsDetailVo.setAttachFile(attachFileVoList);
            }
            return ResultT.success(newsDetailVo);
        } else {
            return ResultT.fail("数据错误");
        }
    }
}
